package cn.tworice.upload.web;

import cn.tworice.common.annotation.NoEncryption;
import cn.tworice.common.util.StringUtils;
import cn.tworice.common.vo.RequestResult;
import cn.tworice.common.vo.StateCodeConst;
import cn.tworice.upload.service.DownloadService;
import cn.tworice.upload.service.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

@Controller
@RequestMapping("/receive")
@Slf4j
@Api(tags = "文件接收")
public class ReceiveController {

    @Resource
    private DownloadService downloadService;

    @Resource
    private UploadUtil uploadUtil;

    @ApiOperation("下载文件")
    @ResponseBody
    @RequestMapping(value = "/fetch/{fileName}", method = RequestMethod.GET)
    @NoEncryption
    public RequestResult fileDownload(@PathVariable String fileName, HttpServletResponse response)throws Exception{
        File downloadFile =  new File(uploadUtil.getUploadPath()+ File.separator + fileName);
        log.info("下载文件的完整路径："+downloadFile.getAbsolutePath());
        if(!downloadFile.exists()){
            log.error("要下载的文件不存在："+downloadFile.getAbsolutePath());
            return RequestResult.error(StateCodeConst.NOT_FOUND,"文件不存在");
        }else {
            //设置响应头，对文件进行url编码
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="+fileName);
            downloadService.outputResponse(downloadFile,response);
            return RequestResult.success();
        }
    }


    @ApiOperation("图片预览")
    @RequestMapping(value = "preview/{fileName}", method = RequestMethod.GET)
    @NoEncryption
    public void preview(@PathVariable String fileName,HttpServletResponse response,@RequestParam(value = "dir",required = false) String dir)throws Exception{
        File downloadFile = new File(uploadUtil.getUploadPath()+ buildPath(dir) + fileName);
        if(!downloadFile.exists()){
            log.error("文件不存在："+downloadFile.getAbsolutePath());
            downloadService.previewNotFound(response);
        }else {
            // 设置响应头，对文件进行url编码
            response.setContentType("image/jpeg");
            downloadService.outputResponse(downloadFile,response);
        }
    }

    @ApiOperation("PDF预览")
    @RequestMapping(value = "pdf/{fileName}", method = RequestMethod.GET)
    @NoEncryption
    public void pdf(@PathVariable String fileName,HttpServletResponse response)throws Exception{
        File downloadFile = new File(uploadUtil.getUploadPath()+ File.separator + fileName);
        if(!downloadFile.exists()){
            log.error("文件不存在："+downloadFile.getAbsolutePath());
            downloadService.previewNotFound(response);
        }else {
            // 设置响应头，对文件进行url编码
            response.setContentType("application/pdf");
            downloadService.outputResponse(downloadFile,response);
        }
    }

    @ApiOperation("MP4视频预览")
    @RequestMapping(value = "video/{fileName}", method = RequestMethod.GET)
    @NoEncryption
    public void video(@PathVariable String fileName, HttpServletResponse response) throws Exception {
        File videoFile = new File(uploadUtil.getUploadPath() + File.separator + fileName);
        if (!videoFile.exists()) {
            log.error("文件不存在：" + videoFile.getAbsolutePath());
            downloadService.previewNotFound(response);
        } else {
            // 设置响应头，对文件进行url编码
            response.setContentType("video/mp4");
            downloadService.outputResponse(videoFile, response);
        }
    }

    private String buildPath(String dir) {
        return StringUtils.isEmpty(dir) ? File.separator : dir + File.separator;
    }

}
